      SUBROUTINE regularizaTikho(X,iflagF,iflagG)	

      !termo de regularizacao: L2 regularizer

      use globais
      implicit none


      integer :: i,j, Ndonoh,iflagF,iflagG
      double precision ::X(N)

      if(iflagF.eq.1 .and. iflagG.eq.0)then !calcula  Treg

	  Treg = 0.0d0
	  do j=2,Nz0-1
	      do i=2,Nx0-1
		  Ndonoh = (j-1)*Nx0 + i
		  Treg = Treg + (X(Ndonoh+1)-X(Ndonoh-1))**2  + (X(Ndonoh+Nx0)-X(Ndonoh-Nx0))**2
	      enddo
	  enddo
	  Treg = Treg*0.5d0/dfloat(Nx0*Nz0)

! 	  Treg = 0.0d0
! 	  do j=1,Nz0-1
! 	      do i=1,Nx0-1
! 		  Ndonoh = (j-1)*Nx0 + i
! 		  Treg = Treg + (X(Ndonoh+1)-X(Ndonoh))**2  + (X(Ndonoh+Nx0)-X(Ndonoh))**2
! 	      enddo
! 	  enddo
! 	  Treg = Treg*0.5d0/dfloat(Nx0*Nz0)

      endif

      if(iflagF.eq.1 .and. iflagG.eq.1)then !calcula  Treg e grad


	  do i=1,Nx0*Nz0
	      gradTreg(i) = 0.0d0
	  enddo
	  Treg = 0.0d0

	  do j=2,Nz0-1
	      do i=2,Nx0-1
		  Ndonoh = (j-1)*Nx0 + i
		  Treg = Treg + (X(Ndonoh+1)-X(Ndonoh-1))**2  + (X(Ndonoh+Nx0)-X(Ndonoh-Nx0))**2
		  gradTreg(Ndonoh+1  ) = gradTreg(Ndonoh+1  ) + (X(Ndonoh+1  )-X(Ndonoh-1))
		  gradTreg(Ndonoh-1  ) = gradTreg(Ndonoh-1  ) - (X(Ndonoh+1  )-X(Ndonoh-1))
		  gradTreg(Ndonoh+Nx0) = gradTreg(Ndonoh+Nx0) + (X(Ndonoh+Nx0)-X(Ndonoh-Nx0))
		  gradTreg(Ndonoh-Nx0) = gradTreg(Ndonoh-Nx0) - (X(Ndonoh+Nx0)-X(Ndonoh-Nx0))
	      enddo
	  enddo
	  Treg = Treg*0.5d0/dfloat(Nx0*Nz0)
	  do i=1,Nx0*Nz0
	      gradTreg(i) = gradTreg(i)/dfloat(Nx0*Nz0)
	  enddo


! 	  do j=1,Nz0-1
! 	      do i=1,Nx0-1
! 		  Ndonoh = (j-1)*Nx0 + i
! 		  Treg = Treg + (X(Ndonoh+1)-X(Ndonoh))**2  + (X(Ndonoh+Nx0)-X(Ndonoh))**2
! 		  gradTreg(Ndonoh+1  ) = gradTreg(Ndonoh+1  ) + (X(Ndonoh+1  )-X(Ndonoh))/dfloat(Nx0*Nz0)
! 		  gradTreg(Ndonoh+Nx0) = gradTreg(Ndonoh+Nx0) + (X(Ndonoh+Nx0)-X(Ndonoh))/dfloat(Nx0*Nz0)
! 		  gradTreg(Ndonoh    ) = gradTreg(Ndonoh+Nx0) - (X(Ndonoh+1  )-X(Ndonoh))/dfloat(Nx0*Nz0) - (X(Ndonoh+Nx0)-X(Ndonoh))/dfloat(Nx0*Nz0)
! 	      enddo
! 	  enddo
! 	  Treg = Treg*0.5d0/dfloat(Nx0*Nz0)


      endif! fim do iflag =2


      return
      end subroutine